package com.ajhlp.spring_modules.db.multiSource;

import java.beans.PropertyVetoException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.ajhlp.spring_modules.db.dao.IDBSourceMetaDao;
import com.ajhlp.spring_modules.db.multiSource.metaModel.DBSource_BasicInfo;
import com.ajhlp.spring_modules.db.multiSource.metaModel.DBSource_DataField;
import com.ajhlp.spring_modules.db.multiSource.metaModel.DBSource_DataSet;
import com.ajhlp.spring_modules.db.multiSource.metaModel.DBSource_Type;
import com.mchange.v2.c3p0.ComboPooledDataSource;

@Service
public class DataSourceCache {
	
	private Logger log = Logger.getLogger(getClass());

	//连接缓存
	private static Map<String,Object> cache = new HashMap<String, Object>();
	
	public static final String DBSOURCE_TYPE_MYSQL = "mysql";
	public static final String DBSOURCE_TYPE_ORACLE = "oracle";
	public static final String DBSOURCE_TYPE_SQLSERVER2000 = "sqlserver2000";
	public static final String DBSOURCE_TYPE_SQLSERVER2003 = "sqlserver2003";
	public static final String DBSOURCE_TYPE_WEBSERVICE = "webService";
	public static final String DBSOURCE_TYPE_FILE = "file";
	
	//查询数据源所属类型
//	@Resource
	private IDBSourceMetaService sourceMetaService;
	
	/**
	 * 获得数据源描述信息服务类
	 * @return
	 */
	public IDBSourceMetaService getSourceMetaService() {
		return sourceMetaService;
	}

	/**
	 * 设置数据源描述信息服务类
	 * @param sourceMetaService
	 */
	public void setSourceMetaService(IDBSourceMetaService sourceMetaService) {
		this.sourceMetaService = sourceMetaService;
	}

	
	public DataSource getDataSource(DBSource_BasicInfo basicInfo) throws SQLException, PropertyVetoException{
//		DriverManagerDataSource dataSource = new DriverManagerDataSource();
//		dataSource.setDriverClassName("com.mysql.jdbc.Driver");
//		dataSource.setUrl("jdbc:mysql://10.71.196.148:3306/city_plat?useUnicode=true&characterEncoding=utf-8");
//		dataSource.setUsername("test1");
//		dataSource.setPassword("123456");
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		
		dataSource.setDriverClass(basicInfo.getDriverClass());
		dataSource.setJdbcUrl(basicInfo.getUrl());
		dataSource.setUser(basicInfo.getUsername());
		dataSource.setPassword(basicInfo.getPassword());
		
		dataSource.setAcquireIncrement(1);
		dataSource.setAcquireRetryAttempts(0);
		dataSource.setAutoCommitOnClose(true);
		
		dataSource.setMaxPoolSize(3);
		dataSource.setMinPoolSize(1);
		dataSource.setInitialPoolSize(1);
		dataSource.setMaxIdleTime(20);
		dataSource.setCheckoutTimeout(10*1000);
		dataSource.setMaxStatements(0);
		return dataSource;
	}
	
	
}
